//
//  JCStatusFrame.swift
//  SinaWeibo_18
//
//  Created by mac on 15/10/17.
//  Copyright © 2015年 cz. All rights reserved.
//

import UIKit


//cell上 子控件 间距
 let CELL_CHILDVIEW_MARGIN: CGFloat = 10
//名称的字体大小
 let CELL_STATUS_NAME_FONT: CGFloat = 14
//创建时间的字体大小
let CELL_STATUS_CREATEDTIME_FONT: CGFloat = 10
//微博内容的字体大小
let CELL_STATUS_TEXT_FONT: CGFloat = 15

//微博frame
class JCStatusFrame: NSObject {

    var status: JCStatus?{
        didSet{
            //计算cell里每一个控件frame
            calcuFrame()
        }
    }
    
    //cell 高度
    var cellHeight: CGFloat?
    //原创微博整体view
    var originalViewF: CGRect?/// 顶部分割线的
    
    //头像
    var headImageViewF: CGRect?
    //名称
    var nameLabelF: CGRect?
    //会员图标
    var vipImageViewF: CGRect?
    
    //创建时间
    var createdTimeLabelF: CGRect?
    //来源
    var sourceLabelF: CGRect?
    //原创微博内容
    var originalTextLabelF: CGRect?
    //原创微博配图
    var originalPhotosF: CGRect?
    
    //转发微博整体View
    var retweetedViewF: CGRect?
    //转发微博内容
    var retweetedTextLabelF: CGRect?
    //转发微博配图
    var retweetedPhotosF: CGRect?
    
    //底部的toolBar
    var statusToolBarF: CGRect?
    
    //计算 frame
    private func calcuFrame(){
        let status = self.status!
        
        let originalViewX: CGFloat = 0
        let originalViewY = CELL_CHILDVIEW_MARGIN
        let originalViewW = ScreenWidth

        //头像frame
        let headImageViewX = CELL_CHILDVIEW_MARGIN
        let headImageViewY = CELL_CHILDVIEW_MARGIN
        let headImageViewSize = CGSizeMake(35, 35)
        headImageViewF = CGRect(origin: CGPointMake(headImageViewX, headImageViewY), size: headImageViewSize)
        
        //名称frame
        let nameLabelX = CGRectGetMaxX(headImageViewF!) + CELL_CHILDVIEW_MARGIN
        let nameLabelY = headImageViewY
        let nameLabelSize = status.user?.name?.size(UIFont.systemFontOfSize(CELL_STATUS_NAME_FONT))
        nameLabelF = CGRect(origin: CGPointMake(nameLabelX, nameLabelY), size: nameLabelSize!)
        
        //会员frame
        if status.user!.isVip{
            //是会员
            let vipImageViewX = CGRectGetMaxX(nameLabelF!) + CELL_CHILDVIEW_MARGIN
            let vipImageViewY = headImageViewY
            let vipImageViewSize = CGSizeMake(nameLabelSize!.height, nameLabelSize!.height)
            vipImageViewF = CGRect(origin: CGPointMake(vipImageViewX, vipImageViewY), size: vipImageViewSize)
        }
        
        
        //创建时间frame
        let createdTimeLabelX = nameLabelX
        let createdTimeLabelSize = status.created_at?.size(UIFont.systemFontOfSize(CELL_STATUS_CREATEDTIME_FONT))
        let createdTimeLabelY = CGRectGetMaxY(headImageViewF!) - createdTimeLabelSize!.height
        createdTimeLabelF = CGRect(origin: CGPointMake(createdTimeLabelX, createdTimeLabelY), size: createdTimeLabelSize!)
        
        //来源frame
        let sourceLabelX = CGRectGetMaxX(createdTimeLabelF!) + CELL_CHILDVIEW_MARGIN
        let sourceLabelY = createdTimeLabelY
        let sourceLabelSize = status.source?.size(UIFont.systemFontOfSize(CELL_STATUS_CREATEDTIME_FONT))
        sourceLabelF = CGRect(origin: CGPointMake(sourceLabelX, sourceLabelY), size: sourceLabelSize!)
        
        //原创微博内容frame
        let originalTextLabelX = CELL_CHILDVIEW_MARGIN
        let originalTextLabelY = CGRectGetMaxY(headImageViewF!) + CELL_CHILDVIEW_MARGIN
        let originalTextLabelSize = status.text?.size(UIFont.systemFontOfSize(CELL_STATUS_TEXT_FONT), constraintedToSize: CGSizeMake(ScreenWidth - CELL_CHILDVIEW_MARGIN*2, CGFloat(MAXFLOAT)))
        originalTextLabelF = CGRect(origin: CGPointMake(originalTextLabelX, originalTextLabelY), size: originalTextLabelSize!)
        //分割线 间距
        var originalViewH = CGRectGetMaxY(originalTextLabelF!) + CELL_CHILDVIEW_MARGIN
        
    
        
        //判断是否有原创配图
        if let originalPicUrls = status.pic_urls where originalPicUrls.count > 0 {
            //原创微博配图的frame
            let originalPhotosX = CELL_CHILDVIEW_MARGIN
            let originalPhotosY = CGRectGetMaxY(originalTextLabelF!) + CELL_CHILDVIEW_MARGIN
       
            //根据图片数量设置  size
            let originalPhotosSize = JCStatusPhotos.countSize(originalPicUrls.count)
            originalPhotosF = CGRect(origin: CGPointMake(originalPhotosX, originalPhotosY), size: originalPhotosSize)
            
            originalViewH = CGRectGetMaxY(originalPhotosF!) + CELL_CHILDVIEW_MARGIN
        
        }
        //设置原创微博整体View的frame
        originalViewF = CGRectMake(originalViewX, originalViewY, originalViewW, originalViewH)
        //根据整体view计算toolbar y值
        var statusToolBarY = CGRectGetMaxY(originalViewF!)
 
        //如果有转发微博  计算frame
        if let retStatus = status.retweeted_status {
            //转发微博整体View
            let retweetedViewX: CGFloat = 0
            let retweetedViewY = CGRectGetMaxY(originalTextLabelF!) + CELL_CHILDVIEW_MARGIN
            let retweetedViewWidth = ScreenWidth
            
            //转发微博内容大小
            let retweetedTextLabelX = CELL_CHILDVIEW_MARGIN
            let retweetedTextLabelY = CELL_CHILDVIEW_MARGIN
            let retweetedTextLabelSize = retStatus.text!.size(UIFont.systemFontOfSize(CELL_STATUS_TEXT_FONT), constraintedToSize: CGSizeMake(ScreenWidth - CELL_CHILDVIEW_MARGIN * 2, CGFloat(MAXFLOAT)))
            retweetedTextLabelF = CGRect(origin: CGPointMake(retweetedTextLabelX, retweetedTextLabelY), size: retweetedTextLabelSize)
            //整体view 高度
            var retweetedViewH = CGRectGetMaxY(retweetedTextLabelF!) + CELL_CHILDVIEW_MARGIN

            
            //判断是否有转发微博配图
            if let retweetedPicUrls = retStatus.pic_urls where retweetedPicUrls.count > 0{
                let retweetedPhotosX = CELL_CHILDVIEW_MARGIN
                let retweetedPhotosY = CGRectGetMaxY(retweetedTextLabelF!) + CELL_CHILDVIEW_MARGIN
                let retweetedPhotosSize = JCStatusPhotos.countSize(retweetedPicUrls.count)
                retweetedPhotosF = CGRect(origin: CGPointMake(retweetedPhotosX, retweetedPhotosY), size: retweetedPhotosSize)
                //整体view 高度
                retweetedViewH = CGRectGetMaxY(retweetedPhotosF!) + CELL_CHILDVIEW_MARGIN
            }
            
            
                        retweetedViewF = CGRect(origin: CGPointMake(retweetedViewX, retweetedViewY), size: CGSizeMake(retweetedViewWidth, retweetedViewH))
            
            //重新赋值 toolbar
            statusToolBarY = CGRectGetMaxY(retweetedViewF!)
        }
        
        
        //底部的toolBar  frame
        let statusToolBarX: CGFloat = 0
        
        let statusToolBarSize = CGSizeMake(ScreenWidth, 35)
        statusToolBarF = CGRect(origin: CGPointMake(statusToolBarX, statusToolBarY), size: statusToolBarSize)
        
        //计算cell的高度
        cellHeight = CGRectGetMaxY(statusToolBarF!) + CELL_CHILDVIEW_MARGIN
    }
}
